<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org" xmlns:shiro="http://www.pollix.at/thymeleaf/shiro">
<head>
    <th:block th:include="include :: header('薪资管理')"/>
    <style>
        #div-content {
            margin: 80px 30px;
            border: 1px solid black;
            font-size: 1.4em;
            padding: 20px;
        }

        #content-title {
            margin: 10px;
        }

        #content div {
            margin: 10px;
        }

        #content .txt {
            width: 90px;
            display: inline-block;
        }

        #content-title {
        }
    </style>
</head>
<body class="gray-bg">
<div class="container-div">
    <div class="row">
        <div class="col-sm-12 search-collapse">
            <form id="formId">
                <div class="select-list">
                    <ul>
                        <li>
                            <label>创建时间：</label>
                            <input type="text" class="time-input" placeholder="请选择创建时间" id="start_time"
                                   name="start_time" autocomplete="off"/>
                            -
                            <input type="text" class="time-input" placeholder="请选择创建时间" id="end_time"
                                   name="end_time" autocomplete="off"/>
                        </li>
                        <li>
                            <label>用户：</label>
                            <select name="userId" id="userId" th:with="users=${@sysuser.getAllUsers()}">
                                <option th:each="user : ${users}" th:text="${user.key}"
                                        th:value="${user.value}"></option>
                            </select>
                        </li>
                        <li>
                            <a class="btn btn-primary btn-rounded btn-sm" onclick="seach_click()"><i
                                    class="fa fa-search"></i>查询</a>
                            <!--                            <a class="btn btn-primary btn-rounded btn-sm" onclick="calculate_click()"><i-->
                            <!--                                    class="fa fa-search"></i>计算</a>-->
                            <a class="btn btn-warning btn-rounded btn-sm" onclick="$.form.reset()"><i
                                    class="fa fa-refresh"></i>重置</a>
                        </li>
                    </ul>
                </div>
            </form>
        </div>
        <div id="div-content">
            <div id="content-title"></div>
            <div id="content">

            </div>
        </div>
    </div>
</div>
<th:block th:include="include :: footer"/>
<script th:src="@{/js/common.js}"></script>
<script th:inline="javascript">
    const prefix = ctx + "system/operation1";
    let xse = 0; // 销售额
    let userId; // 当前选中的userId
    function seach_click() {
        let startTime = $("#start_time").val();
        let endTime = $("#end_time").val();
        userId = $("#userId").val();
        if (!startTime || !endTime || !userId) {
            $.modal.alertError("日期和用户必须填写完整");
            return;
        }

        $.ajax({
            url: prefix + "/getSalesAndRoles",
            type: 'post',
            data: {
                startDate: startTime,
                endDate: endTime,
                userId: userId,
            },
            dataType: 'json',
            success: function (res) {
                $("#content-title").html("");
                $("#content").html("");
                if (res.code != 0) {
                    $.modal.alertWarning(res.msg);
                } else {
                    if (res.data.deptId == 110 || res.data.deptId == 112) {
                        // 医生和药房
                        xse = res.data.amount;
                        $("#content-title").html(`${res.data.userName}: 销售额度(${res.data.amount}元)`);
                        $("#content").html(`
                    <div>
                        <span class="txt">底薪</span>
                        <input type="number" placeholder="请输入底薪" id="dx"/>
                    </div>
                    <div>
                        <span class="txt">基数(%)</span>
                        <input type="number" placeholder="请输入基数" id="js"/>
                    </div>
                    <div>
                        <span class="txt">奖金</span>
                        <input type="number" placeholder="请输入奖金" id="jj"/>
                    </div>
                    <div>
                        <span class="txt">罚款</span>
                        <input type="number" placeholder="请输入罚款" id="fk"/>
                    </div>
                    <div>
                        <span class="txt">薪资</span>
                        <span id="xz"></span>
                    </div>
                    <dvi>
                        <button class="btn btn-primary btn-rounded btn-sm fa fa-search" onclick="calculate_click()">计算</button>
                    </dvi>
`)
                    } else if (res.data.deptId == 111) {
                        // 护士
                        $("#content").html(`
                    <div>
                        <span class="txt">底薪</span>
                        <input type="number" placeholder="请输入底薪" id="dx"/>
                    </div>
                           <div>
                        <span class="txt">护士处置</span>
                        <input type="number" placeholder="请输入护士处置" id="hscz" value="${res.data.amount}"/>
                    </div>
                    <div>
                        <span class="txt">基数(%)</span>
                        <input type="number" placeholder="请输入基数" id="js"/>
                    </div>
                    <div>
                        <span class="txt">注射人数</span>
                        <input type="number" placeholder="请输入注射人数" id="zsrs" value="${res.data.zsrs}"/>
                    </div>
                    <div>
                        <span class="txt">比例</span>
                        <input type="number" placeholder="请输入比例" id="bl"/>
                    </div>
                    <div>
                        <span class="txt">薪资</span>
                        <span id="xz"></span>
                    </div>
                    <dvi>
                        <button class="btn btn-primary btn-rounded btn-sm fa fa-search" onclick="calculate2_click()">计算</button>
                    </dvi>
`)
                    }
                    getbyid(res.data.deptId)
                }
            },
        })
    }

    // 计算医生和药房的薪资
    function calculate_click() {
        // 底薪+A 所销售的×5%+奖金-罚款
        let dx = Number($("#dx").val());
        let js = Number($("#js").val());
        let jj = Number($("#jj").val());
        let fk = Number($("#fk").val());
        let res = dx + xse * js / 100 + jj - fk;
        $("#xz").html(res);
        save({
            userId: Number(userId),
            dixin: dx,
            jishu: js,
            jiangjin: jj,
            fakuan: fk,
        });
    }

    // 计算护士的薪资
    function calculate2_click() {
        // 底薪+护士处置×10%+注射人数×0.2
        let dx = Number($("#dx").val());
        let hscz = Number($("#hscz").val());
        let js = Number($("#js").val());
        let bl = Number($("#bl").val());
        let zsrs = Number($("#zsrs").val());
        let res = dx + hscz * js / 100 + zsrs * bl;
        $("#xz").html(res);
        save({
            userId: Number(userId),
            dixin: dx,
            jishu: js,
            bili: bl,
        });
    }

    function save(data) {
        $.ajax({
            url: ctx + "system/salary/add",
            type: 'post',
            data: data,
            dataType: 'json',
            success: function (res) {
                // console.log(res);
            },
        })
    }

    function getbyid(deptId) {
        $.ajax({
            url: ctx + "system/salary/getbyid/" + userId,
            type: 'post',
            dataType: 'json',
            success: function (res) {
                $("#dx").val(res.data.dixin);
                $("#js").val(res.data.jishu);
                if (deptId == 110 || deptId == 112) {
                    $("#jj").val(res.data.jiangjin);
                    $("#fk").val(res.data.fakuan);
                } else if (deptId == 111) {
                    $("#bl").val(res.data.bili);
                }
            },
        })
    }

    function mysearch() {
        seach_click()
    }
</script>
</body>
</html>